<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_ci.class.php');
require_once('tcms_ci_mod_dbc.class.php');


class TCMS_CI_DBC extends TCMS_CI implements I_DB_Storable {
	public function save(DB_Connection $dbc, $current_user_id = 0) {
		tangra_if_not_int_throw_e($current_user_id);

		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		$ci_mod = new TCMS_CI_Mod_DBC();
		$ci_mod->set_ci($this->get_id());
		$ci_mod->set_modified_by($current_user_id);
		$ci_mod->save($dbc);


		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select type, ".
						"created ".
					"from tcms_cis where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_type($rez_obj->TYPE);
			$this->set_created($rez_obj->CREATED);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_cis_seq');
		$type = $this->get_type();
		if ($this->get_created() != NULL && $this->get_created() != '0000-00-00 00:00:00') {
			$created = $this->get_created();
		} else {
			$created = date('Y-m-d H:i:s');
		}

		if ($id) {
			$sql = "insert into tcms_cis (id, ".
									      "type, ".
									      "created ".
									      ") ".
								"values ".
									      "($id,".
									      "$type, ".
									      "'$created' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_cis_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$type = $this->get_type();
		$created = $this->get_created();

		$sql = "update tcms_cis set ".
						"type = $type, ".
						"created = '$created' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public function get_as_plain_text() {
		return $this->get_id();
	}


	public function get_as_short_plain_text() {
		return $this->get_id();
	}


	public static function get_sql_for_grid($text, $id, $type = 0) {
		$where = TCMS_CI_DBC::prepare_where_clause($text, $id, $type);
		if ($where) {
			$where = " where ".$where;
		}

		if ($text) {
			$sql_suff = ", tcms_cia_text ct".$where." group by ci.id, ci.created, ci.type";
		} else {
			$sql_suff = $where;
		}

		$sql = "select ci.id, ".
						"ci.created, ".
						"ci.type ".
					"from tcms_cis ci ".$sql_suff;

		return $sql;
	}


	public static function	get_sql_count_for_grid($text, $id, $type = 0) {
		$where = TCMS_CI_DBC::prepare_where_clause($text, $id, $type);
		if ($where) {
			$where = " where ".$where;
		}

		if ($text) {
			$sql_suff = ", tcms_cia_text ct".$where;
		} else {
			$sql_suff = $where;
		}

		$sql = "select count(distinct ci.id) as total_rows from tcms_cis ci ".$sql_suff;

		return $sql;
	}


	public static function count_all_for_type(DB_Connection $dbc, $type_id) {
		tangra_if_not_int_throw_e($type_id);

		$sql = "select count(id) as c from tcms_cis where type = $type_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public static function get_sql_select_all_ids_for_type(DB_Connection $dbc, $type_id) {
		tangra_if_not_int_throw_e($type_id);

		$sql = "select id from tcms_cis where type = $type_id";
		return $sql;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_cis where id = $id";
		$dbc->execute($sql);
	}


	private static function remove_duplicate_asteriks($filter) {
		$ret = $filter;
		while(strpos($ret, '**') !== false) {
			$ret = str_replace('**', '*', $ret);
		}

		return $ret;
	}


	private static function prepare_where_clause($text = '', $id = '', $type = 0) {
		$text = TCMS_CI_DBC::remove_duplicate_asteriks($text);

		if ($text || $id || $type) {
			$where = "";
			if ($text) {
				if (strpos($text, '*') !== false) {
					$text = addslashes($text);
					$text = str_replace('*', '%', $text);
					$where .= "ct.value like '$text'";
				} else {
					$text = addslashes($text);
					$where .= "ct.value = '$text'";
				}
			}

			if ($id) {
				if ($where) {
					$where = " and ci.id = $id ";
				} else {
					$where = " ci.id = $id ";
				}
			}

			if ($type) {
				if ($where) {
					$where = " and ci.type = $type ";
				} else {
					$where = " ci.type = $type ";
				}
			}
		} else {
			$where = '';
		}

		return $where;
	}
}